import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
import sys

def predict(model_path, img_path, class_names, threshold=0.5):
    model = tf.keras.models.load_model(model_path)
    img = image.load_img(img_path, target_size=(384, 384))
    x = np.expand_dims(image.img_to_array(img)/255.0, axis=0)

    preds = model.predict(x)[0]
    results = []

    for i, p in enumerate(preds):
        if p >= threshold:
            results.append((class_names[i], float(p)))

    print("🩻 预测结果:")
    if not results:
        print("未检测到显著器官（所有概率 < 阈值）")
    else:
        for name, score in results:
            print(f" - {name}: {score:.3f}")


if __name__ == "__main__":
    model_path = sys.argv[1]
    img_path = sys.argv[2]
    class_names = ["liver", "kidney", "thyroid", "spleen"]
    predict(model_path, img_path, class_names)
